/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.info.service.plugins  
 */
package com.apache.info.service.plugins;

import java.util.List;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.info.entity.PubMetadata;
import com.apache.info.entity.PubSortInfo;
import com.apache.info.util.MessageUtil;

/**
 * description:  动态表执行抽象类
 * @author Hou Dayu 创建时间：2015-8-29  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public abstract class AbstractInfoReleasePlugin implements PluginConnector {

	private IDao pubMetadataDao;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.apache.api.manager.PluginConnector#execute(com.apache.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo vo) throws Exception {
		ResultEntity entity = new ResultEntity();
		entity.setResult("true");
		String sortCode = String.valueOf(vo.getParams("sortCode"));
		ParamsVo pvo = new ParamsVo();
		if (Validator.isNotNull(sortCode)) {
			String cacheKey = "pubSortInfo_" + sortCode;
			MethodParam param = new MethodParam("ById", cacheKey, "", PubSortInfo.class.getName());
			param.setInfoId(sortCode);
			PubSortInfo sort = (PubSortInfo) pubMetadataDao.selectById(param);
			if ("0".equals(sort.getSortStatus())) {
				entity.setMessage(MessageUtil.getMessage("info-release-0002", new String[0], "数据信息不存在或者还没有启用,暂不能使用"));
				return entity;
			}
			if ("1".equals(sort.getSortStatistics())) {
				pvo.setParams("flagInfoStatistics", "true");
			}
			param.setCacheId("");
			param.setParams("sortId", sortCode);
			param.setKey("ByProperty");
			param.setSpanceName(PubMetadata.class.getName());
			List<PubMetadata> list = pubMetadataDao.select(param);
			pvo.setParams("metadatas", list);
			pvo.setParams("sortCode", sortCode);
			vo.setParams("tableName", sort.getInfoTableName());
			pvo.setParams("params", vo.getParams());
			if (Validator.isNotNull(String.valueOf(vo.getParams("dynamicSql")))) {//直接执行sql查询
				pvo.setParams("dyncSql", String.valueOf(vo.getParams("dynamicSql")));
			}

			Object obj = doInvoke(pvo);
			entity.setEntity(obj);
			entity.setMessage("方法执行成功");//////////////////////
		} else {
			entity.setMessage(MessageUtil.getMessage("info-release-0001", new String[0], "执行方法缺少必要参数"));
		}
		return entity;
	}

	protected abstract Object doInvoke(ParamsVo vo);

	public void setPubMetadataDao(IDao pubMetadataDao) {
		this.pubMetadataDao = pubMetadataDao;
	}

}
